package com.im.partchat.ui.fragment.contact;

import android.os.Bundle;
import android.view.View;
import android.widget.ListView;
import android.widget.TextView;

import com.im.partchat.R;
import com.im.partchat.adapter.tree.TreeElement;
import com.im.partchat.adapter.tree.TreeItemAdapter;
import com.im.partchat.base.BaseFragment;
import com.im.partchat.mvp.persenter.ContactPresenter;
import com.im.partchat.utils.Navigation;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

/**
 * Created by hhz on 2017/7/1.
 */
public class PartyFragment extends BaseFragment {
    @BindView(R.id.mymate_list_view)
    ListView mListView;
    @BindView(R.id.empty_img)
    TextView emptyImg;

    private TreeItemAdapter mAdapter;
    private List<TreeElement> mDatas = new ArrayList<>();
    private ContactPresenter presenter;

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_party;
    }

    @Override
    protected void initView(View view, Bundle savedInstanceState) {
        super.initView(view, savedInstanceState);
        mAdapter = new TreeItemAdapter(getActivity(), mListView, mDatas);
        mAdapter.setShowCheckBox(false);
        mAdapter.registerTreeItemClickListener(new TreeItemAdapter.TreeItemClickCallBack() {
            @Override
            public void onTreeItemClick(View view, TreeElement element) {
                Navigation.navigatePerson(element.getPerson().getId());
            }

            @Override
            public void onDeptTreeItemClick(TreeElement items) {
            }

            @Override
            public void onDeptItemChecked(List<TreeElement> items, boolean choosed) {
            }
        });
        mListView.setAdapter(mAdapter);
    }

    @Override
    protected void initData() {
        presenter = new ContactPresenter(this);
        presenter.getOrganization();
    }

    @Override
    public void onLoadDataSuccess(Object o, String flag) {
        List<TreeElement> items = (List<TreeElement>) o;
        if (items != null) {
            mDatas.clear();
            mDatas.addAll(items);
            mAdapter.notifyDataSetChanged();
            if (mDatas.isEmpty()) {
                emptyImg.setVisibility(View.VISIBLE);
            } else {
                emptyImg.setVisibility(View.GONE);
            }
        }
    }

}
